package com.weijianhuawen.wjblog.web.config;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import toolgood.words.IllegalWordsSearch;
import toolgood.words.IllegalWordsSearchResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.function.Function;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description: 敏感词工具包配置类
 * @date: 2024-12-08 23:12
 */

@Configuration
@Slf4j
public class ToolGoodConfig {

    @Bean
    public IllegalWordsSearch illegalWordsSearch(ResourceLoader resourceLoader) throws IOException {
        log.info("【敏感词工具】 初始化....");
        IllegalWordsSearch illegalWordsSearch = new IllegalWordsSearch();

        log.info("【敏感词工具】 加载seni_words.txt文件...");
        // 读取/resource 下的敏感词文件
        List<String> sensitiveWords = Lists.newArrayList();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource("classpath:word/sensi_words.txt").getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isNotBlank(line.trim())) {
                    sensitiveWords.add(line.trim());
                }
            }
        }

        // 设置敏感词
        illegalWordsSearch.UseIgnoreCase = true;
        illegalWordsSearch.UseSkipWordFilter = true;

        illegalWordsSearch.SetSkipWords("的色");
        illegalWordsSearch.SetKeywords(sensitiveWords);
        log.info("【敏感词工具】 初始化敏感词工具成功！");
        return illegalWordsSearch;
    }
}
